#include <iostream>
#include <string>
#include <fstream>
#include <jsoncpp/json/json.h>

struct stu
{
    std::string name;
    int age;
    double weight;

public:
    void debug()
    {
        std::cout << "name : " << name << std::endl;
        std::cout << "age : " << age << std::endl;
        std::cout << "weight : " << weight << std::endl;
    }
};

int main()
{
    // // 结构化数据
    // struct stu zs = {"张三", 18, 70};

    // // 转化为字符串 利用<K,V>来转化
    // Json::Value root;
    // root["name"] = zs.name;
    // root["age"] = zs.age;
    // root["weight"] = zs.weight;

    // // 序列化
    // // Json::FastWriter writer;
    // Json::StyledWriter writer;
    // std::string str = writer.write(root);
    // std::cout << str << std::endl;

    // std::ofstream out("out.txt");
    // if(!out.is_open())
    // {
    //     std::cout << str;
    //     return 1;
    // }
    // out << str;

    // out.close();

    std::ifstream in("out.txt");
    if (!in.is_open())
        return 1;

    char buffer[1024];
    in.read(buffer, sizeof(buffer));
    in.close();

    // 反序列化
    std::string json_string = buffer;
    Json::Value root;
    Json::Reader reader;
    bool res = reader.parse(json_string, root);
    (void)res;

    struct stu zs;
    zs.age = root["age"].asInt();
    zs.name = root["name"].asString();
    zs.weight = root["weight"].asDouble();

    zs.debug();
    return 0;
}